Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Net
Imports System.Text
Imports System.ComponentModel

Namespace C4F.DevKit.WebServices
	''' <summary>
	''' This class is base class for all components in the Coding4Fun Web services library.
	''' </summary>
	<ToolboxItem(False)> _
	Public Class C4FWebServicesBase
		Inherits Component
		#Region "Private Members"
		''' <summary>
		''' Network proxy that a request uses to access the Internet resource. 
		''' The request is made through the proxy server rather than directly to the Internet resource.
		''' </summary>
		Private proxy_Renamed As IWebProxy
		#End Region

		#Region "Public Properties"
		''' <summary>
		''' Network proxy that a request uses to access the Internet resource. 
		''' The request is made through the proxy server rather than directly to the Internet resource.
		''' </summary>
		<Browsable(False)> _
		Public Property Proxy() As IWebProxy
			Get
				Return proxy_Renamed
			End Get
			Set(ByVal value As IWebProxy)
				proxy_Renamed = value
			End Set
		End Property

		#End Region

		#Region "Protected Functions/Methods"
		''' <summary>
		''' Gets the response string for the specified requestUrl
		''' </summary>
		''' <param name="requestUrl">Request Url</param>
		''' <returns>Responsse data</returns>
		''' <exception cref="NotSupportedException">The request scheme specified in requestUrl has not been registered</exception>
		''' <exception cref="ArgumentNullException">requestUrl is a null reference</exception>
		''' <exception cref="UriFormatException">The URI specified in requestUrl is not a valid URI</exception>
		''' <exception cref="WebException">The time-out period for the request expired (OR) An error occurred while processing the request</exception>
		Protected Function GetQueryResponse(ByVal requestUrl As String) As String
			Return GetQueryResponse(requestUrl, String.Empty, String.Empty)
		End Function

		''' <summary>
		''' Gets the response string for the specified requestUrl. Request is authenticated with the specified urlUserName and urlPassword
		''' </summary>
		''' <param name="requestUrl">Request Url</param>
		''' <param name="urlUserName">UserName for Request Url Authentication</param>
		''' <param name="urlPassword">Password for Request Url Authentication</param>
		''' <returns>Responsse data</returns>
		''' <exception cref="NotSupportedException">The request scheme specified in requestUrl has not been registered</exception>
		''' <exception cref="ArgumentNullException">requestUrl is a null reference</exception>
		''' <exception cref="UriFormatException">The URI specified in requestUrl is not a valid URI</exception>
		''' <exception cref="WebException">The time-out period for the request expired (OR) An error occurred while processing the request</exception>
		Protected Function GetQueryResponse(ByVal requestUrl As String, ByVal urlUserName As String, ByVal urlPassword As String) As String
			' Create a web request for input path.
			Dim webRequest As WebRequest = WebRequest.Create(requestUrl)
			If TypeOf webRequest Is HttpWebRequest Then
                CType(webRequest, HttpWebRequest).UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0;)"
			End If

			' If proxy settings have been specified, set the same.
			If Not Me.Proxy Is Nothing Then
				webRequest.Proxy = Me.Proxy
				webRequest.PreAuthenticate = True
			End If
			' If url credentials have been specified, use the same.
			If (Not String.IsNullOrEmpty(urlUserName)) Then
				webRequest.Credentials = New NetworkCredential(urlUserName, urlPassword)
			End If
			' Get response data
			Dim webResponse As WebResponse = webRequest.GetResponse()
			Dim streamReader As StreamReader = New StreamReader(webResponse.GetResponseStream())
			Dim responseData As String = streamReader.ReadToEnd()
			streamReader.Close()

			Return responseData
		End Function
		#End Region
	End Class
End Namespace